package com.essence.common.model;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @brief 字节帧
 * @details 帧头：0x68
 */
public class BytesFrame implements Serializable {
    private static final Logger LOG = LoggerFactory.getLogger(BytesFrame.class);
    public static final int MAX_LENGTH = 253;
    public static final int FLAG = 0x68;

    public enum DirectionEnum {
        Upstream,
        Downstream
    }

    private long id;
    private long substationId;
    private byte[] apduBytes;
    private String srcHost;
    private String dstHost;
    private int srcPort;
    private int dstPort;
    private DirectionEnum direction;
    private Packet packet;
    // 收到报文的时间戳，如果多线程处理需要对时间戳进行排序
    private long timestamp;

    private String framesHex;
    // 数据平面
    private String dataPlane;

    // 构造函数
    public BytesFrame() {
    }

    // 构造函数
    public BytesFrame(long id, byte[] data, String srcHost, String dstHost, int srcPort, int dstPort,
                      DirectionEnum direction, String dataPlane) {
        this.id = id;
        this.srcHost = srcHost;
        this.dstHost = dstHost;
        this.srcPort = srcPort;
        this.dstPort = dstPort;
        this.direction = direction;
        this.apduBytes = new byte[data.length];
        System.arraycopy(data, 0, apduBytes, 0, data.length);
        packet = new Packet(apduBytes);
        timestamp = System.currentTimeMillis();
        this.dataPlane = dataPlane;
    }

    public BytesFrame(byte[] data, String srcHost, String dstHost, int srcPort, int dstPort,
                      DirectionEnum direction, String dataPlane) {
        this(0, data, srcHost, dstHost, srcPort, dstPort, direction, dataPlane);
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getSubstationId() {
        return substationId;
    }

    public void setSubstationId(long substationId) {
        this.substationId = substationId;
    }

    public byte[] getApduBytes() {
        byte[] copy = new byte[apduBytes.length];
        System.arraycopy(apduBytes, 0, copy, 0, copy.length);
        return copy;
    }

    public void setApduBytes(byte[] apduBytes) {
        this.apduBytes = new byte[apduBytes.length];
        System.arraycopy(apduBytes, 0, this.apduBytes, 0, apduBytes.length);
        packet = new Packet(apduBytes);
    }

    public String getSrcHost() {
        return srcHost;
    }

    public void setSrcHost(String srcHost) {
        this.srcHost = srcHost;
    }

    public String getDstHost() {
        return dstHost;
    }

    public void setDstHost(String dstHost) {
        this.dstHost = dstHost;
    }

    public int getSrcPort() {
        return srcPort;
    }

    public void setSrcPort(int srcPort) {
        this.srcPort = srcPort;
    }

    public int getDstPort() {
        return dstPort;
    }

    public void setDstPort(int dstPort) {
        this.dstPort = dstPort;
    }

    public DirectionEnum getDirection() {
        return direction;
    }

    public void setDirection(DirectionEnum direction) {
        this.direction = direction;
    }

    public Packet getPacket() {
        return packet;
    }

    public Packet getAndAnalysePacket() {
        if (packet != null) {
            packet.analyse();
        }
        return packet;
    }

    public void setPacket(Packet packet) {
        this.packet = packet;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public String getDataPlane() {
        return dataPlane;
    }
}
